package com.lalala.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * JDBC工具类
 * <p>基于 druid
 *
 * @author lalala
 * @createdate 2021/9/1 19:20 周三
 */
public class JDBCUtils {
	private static DataSource dataSource;

	static {
		Properties properties = new Properties();
		ClassLoader classLoader = JDBCUtils.class.getClassLoader();
		InputStream resourceAsStream = classLoader.getResourceAsStream("druid.properties");
		try {
			properties.load(resourceAsStream);
			dataSource = DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	public static DataSource getDataSource(){
		return dataSource;
	}
}
